let express = require('express'),
    ejs = require('ejs'),
    app = express(),
    path = require('path'),
    mongoose = require('mongoose'),
    bodyParser = require('body-parser'),
    config = require('./config/config.js'),
    note_class = require('./app/controllers/note-class'),
    note = require('./app/controllers/note'),
    db = mongoose.connection;

//静态资源
app.use(express.static(path.join(__dirname, './app/resources')));

//json中间件
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

//视图配置
app.engine( '.html', ejs.__express );
app.set('view engine', 'html');
app.set('views', path.join(__dirname, './app/views'));


//数据库连接
mongoose.connect(config.host+'/'+config.db);
db.on('error', function (err){
  console.log('数据连接出错'+err);
});
db.on('open', function (){
  console.log('数据库连接成功');
});

//cors跨域
app.all('*', function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'X-Requested-With');
    res.header('Access-Control-Allow-Methods','PUT,POST,GET,DELETE,OPTIONS');
    res.header('X-Powered-By',' 3.2.1')
    res.header('Content-Type', 'application/json;charset=utf-8');
    next();
});

//路由
app.use('/', note_class , note);
//404
app.get('*', (req, res) => {
    res.render('404.html');
});

app.listen(config.port , function(){
  console.log('启动成功');
});
